package com.dayaokeji.rhythmschoolstudent.client.home.meeting;

import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.dayaokeji.rhythmschoolstudent.R;
import com.dayaokeji.rhythmschoolstudent.client.common.ListFragment;
import com.dayaokeji.rhythmschoolstudent.client.home.meeting.adapter.ParticipantsListAdapter;
import com.dayaokeji.rhythmschoolstudent.utils.UserUtils;
import com.dayaokeji.server_api.ApiUtils;
import com.dayaokeji.server_api.api.UserApi;
import com.dayaokeji.server_api.domain.UserInfo;
import com.yqritc.recyclerviewflexibledivider.HorizontalDividerItemDecoration;

import retrofit2.Call;

/**
 * 查询与会者列表
 */
public class ParticipantsListFragment extends ListFragment<UserInfo,BaseViewHolder> {

    private long departmentId = 0;
    private long personalId = 0;
    private long classId = 0;
    private String keyword = "";

    private boolean isQuery = false;

    private OnSelectListener onSelectListener;

    public static ParticipantsListFragment newInstance() {
        
        Bundle args = new Bundle();
        ParticipantsListFragment fragment = new ParticipantsListFragment();
        fragment.setArguments(args);
        return fragment;
    }

    public void queryDepartment(long departmentId) {
        this.departmentId = departmentId;
        isQuery = false;
        onRefresh();
    }

    public void queryPersonal(long departmentId,long personalId) {
        this.departmentId = departmentId;
        this.personalId = personalId;
        isQuery = false;
        onRefresh();
    }

    public void queryClass(long departmentId,long personalId,long classId) {
        this.departmentId = departmentId;
        this.personalId = personalId;
        this.classId = classId;
        isQuery = false;
        onRefresh();
    }

    public void queryKeyword(String keyword) {
        this.keyword = keyword;
        isQuery = true;
        onRefresh();
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        setAdapter(new ParticipantsListAdapter());
        if (getRecyclerView() != null) {
            getRecyclerView().setBackgroundColor(Color.WHITE);
            getRecyclerView().addItemDecoration(new HorizontalDividerItemDecoration.Builder(getActivity())
                    .sizeResId(R.dimen.default_divider_height)
                    .colorResId(R.color.color_divider)
                    .build());
        }

        if (getAdapter() != null) {
            getAdapter().setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
                @Override
                public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                    try{
                        UserInfo userInfo = (UserInfo) adapter.getData().get(position);
                        if (onSelectListener != null) {
                            onSelectListener.onSelect(userInfo);
                        }
                        getAdapter().notifyItemChanged(position);
                    }catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            });

            getAdapter().setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
                @Override
                public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                    try{
                        UserInfo userInfo = (UserInfo) adapter.getData().get(position);
                        if (onSelectListener != null) {
                            onSelectListener.onSelect(userInfo);
                        }
                        getAdapter().notifyItemChanged(position);
                    }catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            });
        }
    }

    @Override
    protected Call createCall(int offset, int max) {
        if (isQuery) {
            return ApiUtils.getApi(UserApi.class).queryKeyword(keyword,keyword,offset,max);
        }else{
            return ApiUtils.getApi(UserApi.class).list(UserUtils.getUserInfo().getUniversityId(),departmentId,personalId,classId,offset,max);
        }
    }

    public void setOnSelectListener(OnSelectListener onSelectListener) {
        this.onSelectListener = onSelectListener;
    }

    public interface OnSelectListener {
        void onSelect(UserInfo userInfo);
    }
}
